﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MTGScore.Resources;

namespace MTGScore
{
    public partial class HelpPage : PhoneApplicationPage
    {
        public HelpPage()
        {
            InitializeComponent();
        }
        Set help;
        Uri currentUri;

        private void HelpPage_Loaded(object sender, RoutedEventArgs e)
        {
            object o;
            if (!PhoneApplicationService.Current.State.TryGetValue("SelectedHelp", out o))
            {
                NavigationService.GoBack();
                return;
            }
            help = (Set)o;
            if (help.helps.Count == 0)
            {
                NavigationService.GoBack();
                return;
            }
            //WebBrowser.Source = help.helps[0].uri;
            bool ok = false;
            for (int i = 0; i < help.helps.Count; i++)
                if (help.helps[i].language == Strings.Main_Language)
                {
                    currentUri = help.helps[i].uri;
                    ok = true;
                    break;
                }
            if (!ok)
                currentUri = help.helps[0].uri;
            GoToUri();
            //WebBrowser.Source = new Uri("http://www.wizards.com/magic/comprules/MagicCompRules_20110401.txt", UriKind.Absolute);

            ////WebBrowser.Source = new Uri("Helps\\MagicCompRules_20110401.txt", UriKind.RelativeOrAbsolute);
            ////WebBrowser.Source = new Uri("Helps\\MagicCompRules_20110401.txt", UriKind.Relative);
            ////WebBrowser.Source = new Uri("it.htm", UriKind.Relative);
            ////WebBrowser.Navigate(new Uri("it.htm", UriKind.Relative));
            ////WebBrowser.Source = new Uri("", UriKind.Absolute);
            ////WebBrowser.Source = new Uri("http://www.google.it", UriKind.Absolute);
            WebBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(WebBrowser_LoadCompleted);
            

            if (help.helps.Count == 1)
            {
                ApplicationBar.IsMenuEnabled = false;
                ApplicationBar.IsVisible = false;
            }
            else
            {
                for (int i = 0; i < help.helps.Count; i++)
                {
                    ApplicationBarMenuItem item = new ApplicationBarMenuItem(help.helps[i].language);
                    item.IsEnabled = true;
                    item.Click += ApplicationBarMenuButton_Click;
                    ApplicationBar.MenuItems.Add(item);
                }
            }
            //if (!App.isTrial())
            //{
            //    AdGrid.Height = 0;
            //    AdControl.Visibility = System.Windows.Visibility.Collapsed;
            //    AdGrid.Visibility = System.Windows.Visibility.Collapsed;
            //    LayoutRoot.RowDefinitions[4].Height = new GridLength(0);
            //}
        }

        private void WebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            ToggleProgressBar.IsEnabled = false;
            ToggleProgressBar.Visibility = System.Windows.Visibility.Collapsed;
            ToggleProgressBar.IsIndeterminate = false;
            EnableSearch = true;
            Searced_Area.IsEnabled = true;
            Search_Button.IsEnabled = true;
            Grid.SetRowSpan(WebBrowser, 2);
        }

        public void GoToUri()
        {
            Grid.SetRowSpan(WebBrowser, 1);
            ToggleProgressBar.IsEnabled = true;
            ToggleProgressBar.Visibility = System.Windows.Visibility.Visible;
            ToggleProgressBar.IsIndeterminate = true;
            EnableSearch = false;
            Searced_Area.IsEnabled = false;
            Search_Button.IsEnabled = false;
            WebBrowser.Source = currentUri;

        }
        private void WebBrowser_Navigating(object sender, NavigatingEventArgs e)
        {
            if (e.Uri != currentUri)

                e.Cancel = true;
        }

        private void ApplicationBarMenuButton_Click(object sender, EventArgs e)
        {
            try
            {
                var item = (ApplicationBarMenuItem)sender;
                for (int i = 0; i < help.helps.Count; i++)
                    if (item.Text == help.helps[i].language)
                    {
                        if (currentUri != help.helps[i].uri)
                        {
                            currentUri = help.helps[i].uri;
                            GoToUri();
                        }
                        return;
                    }
                //if (item.Text == Strings.Match)
                //    PivotControl.SelectedItem = MatchPivot_Name;
                //else if (item.Text == Strings.Mana)
                //    PivotControl.SelectedItem = ManaPivot_Name;
                //else if (item.Text == Strings.History)
                //    PivotControl.SelectedItem = HistoryPivot_Name;
                //else if (item.Text == Strings.Settings)
                //    PivotControl.SelectedItem = SettingsPivot_Name;
                //else if (item.Text == Strings.Helps)
                //    PivotControl.SelectedItem = GuidePivot_Name;

            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }


        #region ACTIVATE_DEACTIVATE

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {

            base.OnNavigatedFrom(e);

            if (this.State.ContainsKey("MTGSCore_SavedHelp"))
            {

                help = (Set)(this.State["MTGSCore_SavedHelp"]);

                this.State.Remove("MTGSCore_SavedHelp");
               
            }
            //else
            //{
            //    NavigationService.GoBack();
            //}
            
        }


        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {

            base.OnNavigatedTo(e);

            this.State.Add("MTGSCore_SavedHelp", help);
        }

        
        #endregion

        bool EnableSearch = false;
        private void Search_Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (EnableSearch)
                    WebBrowser.InvokeScript("search_text", Searced_Area.Text);
            }
            catch (Exception ex)
            {
                ex.ToString();
                MessageBox.Show(Strings.SearchScriptError);
                Searced_Area.IsEnabled = false;
                Search_Button.IsEnabled = false;
            }
        }
        private void Searched_Area_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Search_Button_Click(null, null);
                Focus();
            }
        }
    }
}